--[[
Project: HunterRP
File: chat.lua
Author: Sebihunter
]]--

--Work in Progress

function PrintMessageAll(msgtype, msg)
	for k, v in pairs(player.GetAll()) do
		v:PrintMessage(msgtype, msg)
	end
end

--This does not work
--[[
function AddChatLine( ply, msg )
	datastream.StreamToClients( ply, "AddChatLine", table.concat({msg}, " ") );
end

function AddRadioLine(msg)

end
]]--

function TalkToRange(msg, pos, size)
	local ents = ents.FindInSphere(pos, size)
	for k, v in pairs(ents) do
		if v:IsPlayer() then
			--AddChatLine(ply, msg) This will replace the ChatPrint as soon as it works ;)
			v:ChatPrint(msg)
			v:PrintMessage(HUD_PRINTCONSOLE, msg)
		end
	end
end

function GM:PlayerSay( ply, text )

	if (string.sub(text, 1, 2) == "//") then
		PrintMessageAll(HUD_PRINTTALK, "(OOC) " .. ply:Nick() .. ": "..string.sub( text, 3 ))
        return "";
    end

	if (string.sub(text, 1, 2) == "/r") then
		if ply:Team() == (2 or 3) then
			for k, v in pairs(player.GetAll()) do
				if v:Team() == (2 or 3) then
					v:PrintMessage(HUD_PRINTTALK, "(Radio) " .. ply:Nick() ..": "..string.sub( text, 3 ))
				end
			end	
		end
        return "";
    end

	if (string.sub(text, 1, 2) == "/l") then
		TalkToRange( "(L-OOC) " .. ply:Nick() .. ": " .. string.sub( text, 3 ), ply:GetPos(), 250 )
        return "";
    end	
	
	if (string.sub(text, 1, 2) == "/w") then
		TalkToRange( "(Whisper) " .. ply:Nick() .. ": " .. string.sub( text, 3 ), ply:GetPos(), 50 )
        return "";
    end
	
	if (string.sub(text, 1, 1) == "/") then
		 ply:ConCommand(string.sub( text, 2 ))
        return "";
    end
	
	if( ChatLimit == 1 ) then	
		TalkToRange( "(IC) " .. ply:Nick() .. ": " .. text, ply:GetPos(), 250 )
		return "";	
	end	
	return text;
end